﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraGauges.Win.Gauges.State;
using Zero.POCO.Donnees;
using Zero.POCO.Views;

namespace DevExpress.MailClient.Win.Controls {
    public partial class WidgetCumulMois : UserControl {
        Timer updateTimer = new Timer();
        static Color positiveColor = Color.FromArgb(70, 158, 165);
        static Color negativeColor = Color.FromArgb(165, 70, 113);
        public WidgetCumulMois()
        {
            InitializeComponent();
            updateTimer.Interval = 1000;
            updateTimer.Tick += OnTimerTick;
            updateTimer.Start();
            OnTimerTick(this, EventArgs.Empty);
        }
       
        int index = 0;
       // StockData previousData;
        void OnTimerTick(object sender, EventArgs e) {
          /*  if(IsDisposed) { updateTimer.Stop(); return; }
            StockData data = StockDataCreator.GetData(index);
            if(index > 0) {
                decimal d = data.ClosePrice - previousData.ClosePrice;
                decimal dq = (d * 100) / data.ClosePrice;
                labelControl5.Text = Math.Round( d, 2, MidpointRounding.ToEven).ToString().TrimEnd('0');
                stateIndicatorComponent1.StateIndex =  previousData.ClosePrice - data.ClosePrice < 0 ? 0 : 1;
                if(stateIndicatorComponent1.StateIndex == 0) {
                    labelControl5.ForeColor = positiveColor;
                    labelControl2.Text = data.ClosePrice.ToString().TrimEnd('0') + "<br><color=70,158,165>" + Math.Round(dq, 2, MidpointRounding.ToEven).ToString().TrimEnd('0') + '%';
                }
                else {
                    labelControl5.ForeColor = negativeColor;
                    labelControl2.Text = data.ClosePrice.ToString().TrimEnd('0') + "<br><color=165,70,113>" + Math.Round(dq, 2, MidpointRounding.ToEven).ToString().TrimEnd('0') + '%';
                }
            }
            previousData = data;
            index++;*/
        }
        private void OnCustomDrawElement(object sender, DevExpress.XtraGauges.Core.Primitive.CustomDrawElementEventArgs e) {
            e.Handled = true;
            StateIndicatorComponent indicator = sender as StateIndicatorComponent;
            Point p1 = Point.Empty;
            Point p2 = Point.Empty;
            Point p3 = Point.Empty;
            int height = (int)(e.Info.BoundBox.Width * Math.Sqrt(3) / 2);
            p1.X = (int)(e.Info.BoundBox.Width / 2);
            p2.X = (int)e.Info.BoundBox.Left;
            p3.X = (int)e.Info.BoundBox.Right;
            int y = (int)((e.Info.BoundBox.Height - height) / 2);
            Color stateColor = Color.Empty;
            if(indicator.StateIndex == 0) {
                p1.Y = y;
                p2.Y = p3.Y = y +height;
                stateColor = positiveColor;
            }
            else {
                p1.Y = y + height;
                p2.Y = p3.Y = y;
                stateColor = negativeColor;
            }
            e.Context.Graphics.FillPolygon(new SolidBrush(stateColor), new Point[] { p1, p2, p3 });
        }

        public void UpdateStat(ProfilView profilView)
        {
            ccCumulMois.Series.Clear();
            var series1 = new Series(profilView.ProfilSupportPricncipale.Vague.Libelle, ViewType.Bar);

            series1.Points.Add(new SeriesPoint(profilView.ProfilSupportPricncipale.ProfilPenPrincipale.SupportView.Nom, profilView.ProfilSupportPricncipale.ProfilPenPrincipale.PenetrationMois));
            foreach (var profilSupport in profilView.ProfilSupportPricncipale.ProfilPens)
            {
                series1.Points.Add(new SeriesPoint(profilSupport.SupportView.Nom, profilSupport.PenetrationMois));
            }
            ccCumulMois.Series.Add(series1);



            foreach (var profilSupportSup in profilView.ProfilSupport)
            {
                var series = new Series(profilSupportSup.Vague.Libelle, ViewType.Bar);

                series.Points.Add(new SeriesPoint(profilSupportSup.ProfilPenPrincipale.SupportView.Nom, profilSupportSup.ProfilPenPrincipale.PenetrationMois));
                foreach (var profilSupport in profilSupportSup.ProfilPens)
                {
                    series.Points.Add(new SeriesPoint(profilSupport.SupportView.Nom, profilSupport.PenetrationMois));
                }
                ccCumulMois.Series.Add(series);
            }


           
            
            // Format the the series labels.
            series1.Label.TextPattern = "{A}: {VP:p0}";
        }
    }
}
